/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * MantenimientoEmpresaDeTransporte.java
 *
 * Created on 11-jun-2012, 17:31:21
áclides
 */
package Interfaz_Grafica;

import Logica.*;
import Objetos.*;
import java.awt.Component;
import java.awt.TextField;
import java.awt.Window;
import java.sql.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

public class MantenimientoEmpresaDeTransporte extends javax.swing.JFrame {

    private Component parent;
    private Boolean modificable = false;
    private int Fila;
    private String IdEmpresaTransporte;
    private String mensajeValid = "";
    String codigoEmpresa;

    public String getCodigoEmpresa() {
        return codigoEmpresa;
    }

    public void setCodigoEmpresa(String codigoEmpresa) {
        this.codigoEmpresa = codigoEmpresa;
    }

    public int getFila() {
        return Fila;
    }

    public void setFila(int Fila) {
        this.Fila = Fila;
    }

    public String getIdEmpresaTransporte() {
        return IdEmpresaTransporte;
    }

    public void setIdEmpresaTransporte(String IdEmpresaTransporte) {
        this.IdEmpresaTransporte = IdEmpresaTransporte;
    }

    public String getMensajeValid() {
        return mensajeValid;
    }

    public void setMensajeValid(String mensajeValid) {
        this.mensajeValid = mensajeValid;
    }

    public Boolean getModificable() {
        return modificable;
    }

    public void setModificable(Boolean modificable) {
        this.modificable = modificable;
    }

    /** Creates new form MantenimientoEmpresaDeTransporte */
    public MantenimientoEmpresaDeTransporte() {
        this.setTitle("Mantenimiento de Empresa de Transporte");
        initComponents();      
        //   this.jcbEstado.setVisible(false);               
        DefaultTableModel dtm = new DefaultTableModel();
        String titulos[] = {"Codigo", "RUC", "Nombre de la Empresa", "Razon Social", "Estado"};
        dtm.setColumnIdentifiers(titulos);
        this.dmtEmpresa.setModel(dtm);
        this.btnAgregarEmpresa.setVisible(true);
        setDefaultCloseOperation(DISPOSE_ON_CLOSE);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        txtRazonEmpresa = new java.awt.TextField();
        txtRUCEmpresa = new java.awt.TextField();
        txtNOMBREEmpresa = new java.awt.TextField();
        btnAgregarEmpresa = new javax.swing.JButton();
        btnEliminarEmpresa = new javax.swing.JButton();
        btnModificarEmpresa = new javax.swing.JButton();
        btnBuscarEmpresa = new javax.swing.JButton();
        txtBuscarEmpresa = new java.awt.TextField();
        jcbBuscarEmpresa = new javax.swing.JComboBox();
        jScrollPane1 = new javax.swing.JScrollPane();
        dmtEmpresa = new javax.swing.JTable();
        btnLimpiarEmpresa = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jLabel1.setText("RUC:");

        jLabel2.setText("Nombre:");

        jLabel3.setText("Razón Social:");

        btnAgregarEmpresa.setText("Agregar");
        btnAgregarEmpresa.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAgregarEmpresaActionPerformed(evt);
            }
        });

        btnEliminarEmpresa.setText("Eliminar");
        btnEliminarEmpresa.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnEliminarEmpresaActionPerformed(evt);
            }
        });

        btnModificarEmpresa.setText("Modificar");
        btnModificarEmpresa.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnModificarEmpresaActionPerformed(evt);
            }
        });

        btnBuscarEmpresa.setText("Buscar");
        btnBuscarEmpresa.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnBuscarEmpresaActionPerformed(evt);
            }
        });

        jcbBuscarEmpresa.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Nombre", "RUC" }));

        dmtEmpresa.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        dmtEmpresa.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                dmtEmpresaMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(dmtEmpresa);

        btnLimpiarEmpresa.setText("Limpiar");
        btnLimpiarEmpresa.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnLimpiarEmpresaActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel3)
                                    .addComponent(jLabel1)
                                    .addComponent(jLabel2))
                                .addGap(49, 49, 49)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(txtRUCEmpresa, javax.swing.GroupLayout.PREFERRED_SIZE, 97, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addGroup(layout.createSequentialGroup()
                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                            .addComponent(txtNOMBREEmpresa, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                            .addComponent(txtRazonEmpresa, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 97, javax.swing.GroupLayout.PREFERRED_SIZE))
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 158, Short.MAX_VALUE)
                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                            .addComponent(btnModificarEmpresa, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                            .addComponent(btnAgregarEmpresa, javax.swing.GroupLayout.DEFAULT_SIZE, 101, Short.MAX_VALUE)
                                            .addComponent(btnEliminarEmpresa, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                                        .addGap(9, 9, 9))))
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                .addGap(367, 367, 367)
                                .addComponent(btnLimpiarEmpresa, javax.swing.GroupLayout.DEFAULT_SIZE, 99, Short.MAX_VALUE)
                                .addGap(12, 12, 12)))
                        .addGap(9, 9, 9))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(btnBuscarEmpresa)
                        .addGap(81, 81, 81)
                        .addComponent(txtBuscarEmpresa, javax.swing.GroupLayout.PREFERRED_SIZE, 103, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(143, 143, 143)
                        .addComponent(jcbBuscarEmpresa, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap(24, Short.MAX_VALUE))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(41, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                        .addGroup(layout.createSequentialGroup()
                            .addComponent(jLabel1)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                            .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 21, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGroup(layout.createSequentialGroup()
                            .addComponent(txtRUCEmpresa, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGap(10, 10, 10)
                            .addComponent(txtNOMBREEmpresa, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(2, 2, 2)
                        .addComponent(btnAgregarEmpresa)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnModificarEmpresa)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 21, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtRazonEmpresa, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnEliminarEmpresa))
                .addGap(50, 50, 50)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(btnBuscarEmpresa)
                    .addComponent(txtBuscarEmpresa, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jcbBuscarEmpresa, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 147, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(btnLimpiarEmpresa)
                .addGap(30, 30, 30))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void dmtEmpresaMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_dmtEmpresaMouseClicked
// TODO add your handling code here:

    int row = this.dmtEmpresa.rowAtPoint(evt.getPoint());
    if (row >= 0) {
        this.setModificable(true);
        this.modificable = true;
        this.setFila(row);
        this.txtRUCEmpresa.setText(this.dmtEmpresa.getModel().getValueAt(row, 1).toString());
        this.txtRazonEmpresa.setText(this.dmtEmpresa.getModel().getValueAt(row, 3).toString());
        //String titulos[] = {"Codigo", "Nombre", "RUC", "Razon Social", "Estado"};
        //this.txtApMaterno.setText(this.dtmTablaEmpresa.getModel().getValueAt(row, 3).toString());
        //this.txtApPaterno.setText(this.dtmTablaEmpresa.getModel().getValueAt(row, 2).toString());
        this.txtNOMBREEmpresa.setText(this.dmtEmpresa.getModel().getValueAt(row, 2).toString());
        this.setCodigoEmpresa(this.dmtEmpresa.getModel().getValueAt(row, 0).toString());
    } else {
        this.setModificable(false);
    }

}//GEN-LAST:event_dmtEmpresaMouseClicked

private void btnEliminarEmpresaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnEliminarEmpresaActionPerformed
    int resp;
    LEmpresaDeTransporte objLEmpresaDeTransporte = new LEmpresaDeTransporte();
    String mensaje = "fallo";
    int codigo;
    if (this.modificable == true) {
        resp = JOptionPane.showConfirmDialog(null, "¿Desea Eliminar el registro?", "Pregunta", 0);
        if (resp == 0) {
            int row = this.getFila();
            if (row >= 0) {
                try {
                    // JOptionPane.showMessageDialog(this, this.dtmTablaPersonas.getValueAt(row, 0));
                    codigo = Integer.parseInt(this.dmtEmpresa.getValueAt(row, 0).toString().trim());

                    mensaje = objLEmpresaDeTransporte.EliminarEmpresa(codigo);


                    //this.LimpiarTabla();
                } catch (SQLException ex) {
                    Logger.getLogger(MantenimientoEmpresaDeTransporte.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
            JOptionPane.showMessageDialog(null, mensaje,
                    "Información", JOptionPane.INFORMATION_MESSAGE);
        }

    } else {
        mensaje = "Debe de selecionar una Fila!";
        JOptionPane.showMessageDialog(null, mensaje,
                "Información", JOptionPane.INFORMATION_MESSAGE);
    }
    // TODO add your handling code here:
}//GEN-LAST:event_btnEliminarEmpresaActionPerformed

private void btnAgregarEmpresaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAgregarEmpresaActionPerformed
    int resp;
    Boolean validPage = true;
    validPage = ValidarPagina();

    resp = JOptionPane.showConfirmDialog(null, "¿Desea Agregar el registro?", "Pregunta", 0);
    if (validPage) {
        if (resp == 0) {
            try {
                //      LimpiarTabla();
                EmpresaDeTransporte objEmpresaDeTransporte = new EmpresaDeTransporte();
                objEmpresaDeTransporte.setEstado("1");
                objEmpresaDeTransporte.setIdEmpresaTransporte(ObtenerIdEmpresaTransporte());
                objEmpresaDeTransporte.setNombre(this.txtNOMBREEmpresa.getText());
                objEmpresaDeTransporte.setRUC(this.txtRUCEmpresa.getText());
                objEmpresaDeTransporte.setRazonSocial(this.txtRazonEmpresa.getText());
                LEmpresaDeTransporte objLEmpresaDeTransporte = new LEmpresaDeTransporte();

                String mensaje = "Fallo";
                try {
                    mensaje = objLEmpresaDeTransporte.AgregarEmpresa(objEmpresaDeTransporte);
                } catch (SQLException ex) {
                    Logger.getLogger(MantenimientoDePersonas.class.getName()).log(Level.SEVERE, null, ex);
                }
                JOptionPane.showMessageDialog(null, mensaje,
                        "Información", JOptionPane.INFORMATION_MESSAGE);
            } catch (SQLException ex) {
                Logger.getLogger(MantenimientoEmpresaDeTransporte.class.getName()).log(Level.SEVERE, null, ex);
            }

        }
    } else {
        JOptionPane.showMessageDialog(null, this.getMensajeValid(), "Información", JOptionPane.INFORMATION_MESSAGE);
    }
    // TODO add your handling code here:
}//GEN-LAST:event_btnAgregarEmpresaActionPerformed

private void btnModificarEmpresaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnModificarEmpresaActionPerformed
// TODO add your handling code here:
    int resp;
    if (this.modificable == true) {
        resp = JOptionPane.showConfirmDialog(null, "¿Desea Modificar el registro?", "Pregunta", 0);
        if (resp == 0) {
            try {
                EmpresaDeTransporte objEmpresaDeTransporte = new EmpresaDeTransporte();
                objEmpresaDeTransporte.setIdEmpresaTransporte(this.getIdEmpresaTransporte());
                objEmpresaDeTransporte.setEstado("1");
                objEmpresaDeTransporte.setNombre(this.txtNOMBREEmpresa.getText());
                objEmpresaDeTransporte.setRUC(this.txtRUCEmpresa.getText());
                objEmpresaDeTransporte.setRazonSocial(this.txtRazonEmpresa.getText());
                LEmpresaDeTransporte objLEmpresaDeTransporte = new LEmpresaDeTransporte();
                String mensaje = "Fallo";

                mensaje = objLEmpresaDeTransporte.ModificarEmpresa(objEmpresaDeTransporte);

                JOptionPane.showMessageDialog(null, mensaje,
                        "Información", JOptionPane.INFORMATION_MESSAGE);
            } catch (SQLException ex) {
                Logger.getLogger(MantenimientoEmpresaDeTransporte.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        this.setModificable(false);
        //      this.limpiar();
    } else {
        JOptionPane.showMessageDialog(null, "Debe selecionar un registro de la tabla!", "Información", JOptionPane.INFORMATION_MESSAGE);
    }
    // TODO add your handling code here:
}//GEN-LAST:event_btnModificarEmpresaActionPerformed

private void btnBuscarEmpresaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnBuscarEmpresaActionPerformed
    LEmpresaDeTransporte objLEmpresaDeTransporte = new LEmpresaDeTransporte();
    String DNI;

    if (this.txtBuscarEmpresa.getText().equals("") == true) {

        this.dmtEmpresa.setModel(objLEmpresaDeTransporte.MostrarEmpresas());


    } else {/// aquiiiiiiiiiiiii errrorrr    
        dmtEmpresa.setModel(objLEmpresaDeTransporte.SelectEmpresas(this.txtBuscarEmpresa.getText(), this.jcbBuscarEmpresa.getSelectedItem().toString(), this.txtBuscarEmpresa.getText()));
    }
    // TODO add your handling code here:
}//GEN-LAST:event_btnBuscarEmpresaActionPerformed

private void btnLimpiarEmpresaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnLimpiarEmpresaActionPerformed
// TODO add your handling code here:

    DefaultTableModel dtm = new DefaultTableModel();
    String titulos[] = {"Codigo", "RUC", "Nombre de la Empresa", "Razon Social", "Estado"};
    dtm.setColumnIdentifiers(titulos);
    this.dmtEmpresa.setModel(dtm);
}//GEN-LAST:event_btnLimpiarEmpresaActionPerformed

    private String ObtenerIdEmpresaTransporte() throws SQLException {
        LEmpresaDeTransporte objEmpresaDeTransporte = new LEmpresaDeTransporte();
        return objEmpresaDeTransporte.ObtenerSecuenciaIdEmpresa();
    }

    /**
     * @param args the command line arguments
     */
    private Boolean ValidarPagina() {
        String cad;
        cad = this.txtRUCEmpresa.getText();
        if (cad.equals("") == true) {
            this.setMensajeValid("Debe ingresar un número en el campo RUC");
        }

        for (int i = 0; i < cad.length(); i++) {
            if (!Character.isDigit(cad.charAt(i))) {
                this.setMensajeValid("Debe ingresar un número en el campo RUC");
                return false;
            }
        }

        cad = this.txtNOMBREEmpresa.getText();
        if (cad.equals("") == true) {
            this.setMensajeValid("Debe ingresar datos en el campo NOMBRE");
        }
        for (int i = 0; i < cad.length(); i++) {
            if (!((cad.charAt(i) > 64 && cad.charAt(i) < 91)
                    || (cad.charAt(i) > 96 && cad.charAt(i) < 123))) {
                this.setMensajeValid("Debe ingresar un número en el campo RUC");
                return false;
            }
        }


        return true;


    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAgregarEmpresa;
    private javax.swing.JButton btnBuscarEmpresa;
    private javax.swing.JButton btnEliminarEmpresa;
    private javax.swing.JButton btnLimpiarEmpresa;
    private javax.swing.JButton btnModificarEmpresa;
    private javax.swing.JTable dmtEmpresa;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JComboBox jcbBuscarEmpresa;
    private java.awt.TextField txtBuscarEmpresa;
    private java.awt.TextField txtNOMBREEmpresa;
    private java.awt.TextField txtRUCEmpresa;
    private java.awt.TextField txtRazonEmpresa;
    // End of variables declaration//GEN-END:variables
}
